/** Simple game demo using
 * JGE. Comments should hopefully
 * describe what the functions do.
 * 
 * Most of them should hopefully
 * be self explanatory.
 * 
 * @author Andrew
 * LAST UPDATED - Andrew 5/2/12
 */

package game;

// Needed imports
import jge.JGE;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.LWJGLException;



public class Game {
    
    // The main function. This is called once and runs until the end.
    public void start() {
        try {
            // Create new window with width 900, height 800
            Display.setDisplayMode(new DisplayMode(900,800));
            Display.create();
        } catch (LWJGLException e) {    // If theres an error
            e.printStackTrace();        // print it
            System.exit(0);             // and close the window and program
        }
        
        // Initialize the window
        JGE.initJGE();            
                
        
        // set the base values
        double angle = 56.5;
        double vel = 10.0;
        
        // No ball is being fired at the start
        boolean ballfire = false;
        
        // create a simple box, which is our background, at 0,0
        jge.SimpleObject bg = JGE.newObject(0, 0, "testbg.png");
        
        // Create a tank to be the player at 0,0
        jge.TankObject you = JGE.newTank(0, 0);
        
        // Create a tank to be the target some distance away from the player.
        jge.TankObject target = JGE.newTank(720, 0);
        // Reverse it so it looks at the player
        target.reverse();
        
        // create a projectile at the position of the player with the starting
        // velocity and angle.
        jge.ProjectileObject ball = JGE.newMissile(you.getWidth(), you.getHeight(), vel, angle);
        
        
        // While the user has not closed the window...
        while (!Display.isCloseRequested()) {
            
            // Reset the screen
            JGE.glBufferReset();
            
            // If the up arrow is pressed increase the angle, unless it is greater
            // than 90, in which case keep it as 90.
            if(Keyboard.isKeyDown(Keyboard.KEY_UP)) {
                if(angle >= 90) {
                    angle = 90;
                }
                else angle += 0.5;
            }
            // If down arrow is pressed decrease the angle.
              if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
                angle -= 0.5;
              }
            // If the Right arrow is pressed increase the velocity
              if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
                vel += 0.01;
              }
            // If the left arrow is pressed decrease the velocity
              if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
                vel -= 0.01;
              }
              
            // If the s key is pressed, output the Angle and velocity in the
            // terminal (probably should be on screen)
              if(Keyboard.isKeyDown(Keyboard.KEY_S)) {
                  System.out.println("Angle: " + angle);
                  System.out.println("Velocity: " + vel); 
              }
              
            // if a ball is not currently being fired
              if(ballfire == false) {
              
              // If space is pressed, fire a ball at the current angle
              // and velocity and tell the program you are firing a ball
              if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
                  ball.setAngle(angle);
                  ball.setVelocity(vel);
                  ballfire = true;
                  
              }
              // draw the background first
              bg.draw();
              // then the player
              you.draw();
              // the target
              target.draw();
              // and update the screen so it can be seen
              Display.update();   
              
              // Note: do not draw the ball as it hasnt been fired!
            }
            // If a ball IS being fired
            else {
                  // draw the Background, the player and the target
                  bg.draw();
                  you.draw();
                  target.draw();
                  // If the ball is still on the screen and has not
                  // hit the target, then change the position of the ball
                  // draw it and increase its time (ball.update() does all
                  // of this).
                  if(ball.checkCollision(target) == false &&
                     ball.inBounds(900,800)) {
                      ball.update();
                     }
                  // If the ball is off the screen or has hit the target
                  // Reset the balls time and position, and allow another
                  // ball to be fired.
                  else {
                      ball.reset();
                      ballfire = false;
                  }
                  // But either way update the display
                  Display.update();
            }
        }
        // Once close is requested, destroy the screen
        Display.destroy();
    }
    
    // On program run, make the game and start it.
    public static void main(String[] argv) {
        Game app = new Game();
        app.start();
    }
}
